/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client.check;

import gwt.validator.client.exception.ValidationIssue;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBoxBase;

/**
 * Validator for checking a String's length.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class StringLengthRangeCheck extends BaseCheck {

	/**
	 * The minimum valid length of the string
	 */
	private int minLength;

	/**
	 * The maximum valid length of the string
	 */
	private int maxLength;

	/**
	 * The text box that displays the text to be validated
	 */
	private TextBoxBase textBox;

	/**
	 * Constructor
	 * 
	 * @param aTextBox
	 *            The text box that displays the text to be validated
	 * @param aLabel
	 *            The label that displays the property's name
	 * @param aMinLength
	 *            The minimum valid length of the string
	 * @param aMaxLength
	 *            The maximum valid length of the string
	 */
	public StringLengthRangeCheck(TextBoxBase aTextBox, Label aLabel,
			int aMinLength, int aMaxLength) {
		super(aLabel);

		checkNotNull(aTextBox);
		checkMinValue(aMinLength, 0);
		checkMinValue(aMaxLength, 0);

		this.textBox = aTextBox;
		this.minLength = aMinLength;
		this.maxLength = aMaxLength;
	}

	/**
	 * Constructor
	 * 
	 * @param aTextBox
	 *            The text box that displays the text to be validated
	 * @param aPropertyName
	 *            The property's name
	 * @param aMinLength
	 *            The minimum valid length of the string
	 * @param aMaxLength
	 *            The maximum valid length of the string
	 */
	public StringLengthRangeCheck(TextBoxBase aTextBox, String aPropertyName,
			int aMinLength, int aMaxLength) {
		super(aPropertyName);

		checkNotNull(aTextBox);
		checkMinValue(aMinLength, 0);
		checkMinValue(aMaxLength, 0);

		this.textBox = aTextBox;
		this.minLength = aMinLength;
		this.maxLength = aMaxLength;
	}

	public int getMaxLength() {
		return maxLength;
	}

	public int getMinLength() {
		return minLength;
	}

	public List validateImpl() {
		List res = new ArrayList();

		String txt = this.textBox.getText();
		int length = txt.length();
		if (length < this.minLength) {
			res.add(new ValidationIssue(this.getIssueMessageFactory()
					.stringLengthTooShort(this.getPropertyName(), txt, length,
							this.minLength)));
		} else if (length > this.maxLength) {
			res.add(new ValidationIssue(this.getIssueMessageFactory()
					.stringLengthTooLong(this.getPropertyName(), txt, length,
							this.maxLength)));
		}

		return res;
	}
}
